﻿using LuDragon.IService;
using LuDragon.Data.DataHandler;
using System.Collections.Generic;
using System.Data;

namespace LuDragon.Service
{
    public class DapperBaseService : IDapperBaseService
    {
        protected readonly DapperDataServer _dataServer;

        public DapperBaseService()
        {
            _dataServer = new DapperDataServer();
        }

        #region WriteDB

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">表实体</param>
        /// <param name="dbWrite">写数据库连接</param>
        /// <param name="transaction">事物</param>
        /// <returns></returns>
        public void Add<TModel>(TModel model, IDbConnection dbWrite = null, IDbTransaction transaction = null) where TModel : class
        {
            _dataServer.Add(model, dbWrite, transaction);
        }

        /// <summary>
        /// 批量添加数据信息
        /// </summary>
        /// <param name="list">实体集合</param>
        /// <param name="dbWrite">写数据库连接</param>
        /// <param name="transaction">事物</param>
        /// <returns></returns>
        public void Add<TModel>(IEnumerable<TModel> list, IDbConnection dbWrite = null,
            IDbTransaction transaction = null) where TModel : class
        {
            _dataServer.Add(list, dbWrite, transaction);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model">表实体</param>
        /// <param name="dbWrite">写数据库连接</param>
        /// <param name="transaction">事物</param>
        /// <returns></returns>
        public void Update<TModel>(TModel model, IDbConnection dbWrite = null, IDbTransaction transaction = null) where TModel : class
        {
            _dataServer.Update(model, dbWrite, transaction);
        }

        /// <summary>
        /// 批量更新
        /// </summary>
        /// <param name="list">实体集合</param>
        /// <param name="dbWrite">写数据库连接</param>
        /// <param name="transaction">事物</param>
        /// <returns></returns>
        public void Update<TModel>(IEnumerable<TModel> list, IDbConnection dbWrite = null,
            IDbTransaction transaction = null) where TModel : class
        {
            _dataServer.Update(list, dbWrite, transaction);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="model">表实体</param>
        /// <param name="dbWrite">写数据库连接</param>
        /// <param name="transaction">事物</param>
        /// <returns></returns>
        public void Delete<TModel>(TModel model, IDbConnection dbWrite = null, IDbTransaction transaction = null) where TModel : class
        {
            _dataServer.Delete(model, dbWrite, transaction);

        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="list">实体集合</param>
        /// <param name="dbWrite">写数据库连接</param>
        /// <param name="transaction">事物</param>
        /// <returns></returns>
        public void Delete<TModel>(IEnumerable<TModel> list, IDbConnection dbWrite = null,
            IDbTransaction transaction = null) where TModel : class
        {
            _dataServer.Delete(list, dbWrite, transaction);
        }

        /// <summary>
        /// 执行sql语句
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="dbWrite">写数据库连接</param>
        /// <param name="transaction">事物</param>
        /// <returns></returns>
        public int Execute(string sql, IDbConnection dbWrite = null, IDbTransaction transaction = null)
        {
            return _dataServer.Execute(sql, dbWrite, transaction);
        }

        #endregion

        #region ReadDB

        /// <summary>
        /// 获取实体数据信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public TModel GetModel<TModel>(object id) where TModel : class
        {
            return _dataServer.GetModel<TModel>(id);
        }

        /// <summary>
        /// 获取实体所有数据信息列表
        /// </summary>
        /// <returns></returns>
        public IEnumerable<TModel> GetAll<TModel>() where TModel : class
        {
            return _dataServer.GetAll<TModel>();
        }

        /// <summary>
        /// 查询数据列表
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns></returns>
        public IEnumerable<T> Query<T>(string sql, object param = null) where T : class
        {
            return _dataServer.Query<T>(sql, param);
        }    
        #endregion
    }
}